package com.aisino.efence.utils;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

/**
 * @author fzc
 * @package com.aisino.efence.utils
 * 压缩工具类
 * @create 2018-04-13 上午9:20
 **/
public class CompressUtil {

    /**
     * 7z 文件后缀
     */
    private static final String FORMAT_7Z_FILE = ".7z";


    /**
     * 7z 压缩文件方法
     *
     * @param sourcePath     源文件路径
     * @param sourceFileName 源文件名称
     * @param fileRename     源文件名称重命名
     * @param targetPath     压缩后文件路径
     * @param targetFileName 压缩后文件名称
     */
    public static void compressFileBy7z(String sourcePath, String sourceFileName, String fileRename, String targetPath, String targetFileName) {
        if (StringUtils.isBlank(targetFileName) || !targetFileName.endsWith(FORMAT_7Z_FILE)) {
            throw new RuntimeException("压缩后文件名称不正确");
        }
        // 源文件
        String oriFilePathAndName = sourcePath + sourceFileName;
        // 压缩后文件
        String afterOriFilePathAndName = targetPath + targetFileName;
        //压缩
        try (SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File(afterOriFilePathAndName))) {
            SevenZArchiveEntry entry;
            try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oriFilePathAndName))) {
                if (fileRename != null) {
                    entry = sevenZOutput.createArchiveEntry(new File(oriFilePathAndName), fileRename);
                } else {
                    entry = sevenZOutput.createArchiveEntry(new File(oriFilePathAndName), sourceFileName);
                }
                sevenZOutput.putArchiveEntry(entry);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = bis.read(buffer)) > 0) {
                    sevenZOutput.write(buffer, 0, len);
                }
                sevenZOutput.closeArchiveEntry();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }


    /**
     * 7z 解压方法
     *
     * @param ori7zFilePath
     * @param dir
     * @param fileName
     */
    public static void unCompressFileBy7z(String ori7zFilePath, String dir, String fileName) {
        if (ori7zFilePath.endsWith(FORMAT_7Z_FILE)) {
            throw new RuntimeException("文件格式不正确");
        }
        File tempFile = new File(ori7zFilePath);
        try (FileOutputStream outputStream = new FileOutputStream(new File(dir + fileName));
             SevenZFile sevenZFile = new SevenZFile(tempFile)) {
            SevenZArchiveEntry entry = sevenZFile.getNextEntry();
            byte[] content = new byte[(int) entry.getSize()];
            int count;
            while ((count = sevenZFile.read(content, 0, (int) entry.getSize())) > 0) {
                outputStream.write(content, 0, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        String oriFilePath = "/Users/fzc/Desktop/tmp/";
        String oriFileName = "nuonuo-20180418.encrypt";
        String afterOriFilePath = "/Users/fzc/Desktop/tmp/";
        String afterOriFileName = "nuonuo-20180418.7z";
        try {
            compressFileBy7z(oriFilePath, oriFileName, "nuonuo-20180418.csv",afterOriFilePath, afterOriFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
